<?php
namespace app\appoint\controller;
use app\AdminRest;
use think\App;
use think\facade\Db;
use think\Request;
use app\appoint\model\AdminProject as Pro_model;
use app\appoint\model\AdminCate;
class AdminProject extends AdminRest
{

    protected $model;
    protected $cate_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model      = new Pro_model();
        $this->cate_model = new AdminCate();
    }
    /**
     * 预约服务列表
     */
    public function proList(){
        $input = $this->_input;

        $data = $this->model->proList(['uniacid'=>$this->_uniacid],$input['limit']);
        return $this->success($data);
    }

    /**
     *
     * 添加预约服务
     */
    public function proAdd(){
        $input = $this->_input;
        $input = $this->returnData($input);
        if(!empty($input['times_start'])&&strlen($input['times_start'])>300){
            return $this->error('添加预约时间段过多');
        }
        $res   = $this->model->proAdd($input);
        return $this->success($res);
    }

    /**
     * @param $input
     * @return mixed
     * 转换插入数据
     */
    public function returnData($input){
        $input['uniacid']     = $this->_uniacid;
        $input['days']        = implode(',',$input['days']);
        $input['carousel']    = implode(',',$input['carousel']);
        $input['cover']       = implode(',',$input['cover']);
        $input['times_start'] = $this->getTimes($input['time_ranges'],0);
        $input['times_end']   = $this->getTimes($input['time_ranges'],1);
        unset($input['time_ranges']);
        return $input;
    }
    /**
     * @param $date
     * @param $type
     * @return array|string
     * 转换数据
     */
    public function getTimes($date,$type){
        $data = [];
        if(!empty($date)){
            foreach ($date as $value){
                $value = explode('-',$value);
                $data[] = $value[$type];
            }
        }
        return !empty($data)?implode(',',$data):$data;
    }
    /**
     * @return \think\Response
     * 删除服务
     */
    public function prodelete(){
        $input = $this->_input;
        $res   = $this->model->proUpdate(['id'=>$input['id']],$input);
        return $this->success($res);
    }
    /**
     *
     * 编辑预约服务
     */
    public function proUpdate(){
        $input = $this->_input;
        if(count($input)>1){
            $input = $this->returnData($input);

            if(!empty($input['times_start'])&&strlen($input['times_start'])>300){

                return $this->error('添加预约时间段过多');
            }
            $data  = $this->model->proUpdate(['id'=>$input['id']],$input);
        }else{
            $data  = $this->model->proInfo(['id'=>$input['id']]);
        }
        return $this->success($data);
    }


    /**
     * @return \think\Response
     * 修改状态
     */
    public function changeStatus(){
        $input = $this->_input;
        $data  = $this->model->proUpdate(['id'=>$input['id']],$input);
        return $this->success($data);

    }
    /**
     *
     * 预约设置
     */
    public function proSetting(){
        $data['cate'] = $this->cate_model->cateSelect(['uniacid'=>$this->_uniacid,'status'=>1]);
        return $this->success($data);
    }

    /**
     * 预约配置
     *
     */
    public function appConfig(){
        $input = $this->_input;
        $data  = Db::name('longbing_card_config')->where(['uniacid'=>$this->_uniacid])->field('id,appoint_pic,appoint_name')->find();
        $data  = transImagesOne($data,['appoint_pic']);
        if(count($input)>0){
//            $input['appoint_pic'] = implode(',',$input['appoint_pic']);
            if(empty($data)){
                $input['uniacid']     = $this->_uniacid;
                $input['create_time'] = time();
                Db::name('longbing_card_config')->insert($input);
            }else{
                $input['update_time'] = time();
                Db::name('longbing_card_config')->where(['uniacid'=>$this->_uniacid])->update($input);
            }
        }
        return $this->success($data);
    }


    /**
     *
     * 预约时间配置
     */

    public function formatRange(){
        $input = $this->_input;
        $time_start = intval($input[ 'time_start' ]);
        $time_range = intval($input[ 'time_range' ]);
        $time_start = $time_start ? $time_start : 0;
        $time_range = $time_range ? $time_range : 30;
        $time_range = $time_range * 60;
        $today_time    = strtotime( date( 'Y-m-d' ) );
        $tomorrow_time = strtotime( date( 'Y-m-d', strtotime( "+1 day" ) ) );
        if ( strlen( $time_start ) == 1 ) {
            $today_time = date( 'Y-m-d', $today_time ) . ' 0' . $time_start . ':00:00';
        }
        else {
            $today_time = date( 'Y-m-d', $today_time ) . ' ' . $time_start . ':00:00';
        }
        $today_time = strtotime( $today_time );
        $tmp_time   = $today_time;
        $time_array = array();
        for ( $i = 0; $i < 100; $i++ ) {
            if ( $tmp_time >= $tomorrow_time ) {
                break;
            }
            $tmp      = date( 'H:i', $tmp_time ) . '-' . date( 'H:i', $tmp_time + $time_range );
            $tmp_time += $time_range;
            array_push( $time_array, $tmp );
        }

        $data = array(
            'today_time'    => $today_time,
            'tomorrow_time' => $tomorrow_time,
            'time_array'    => $time_array,
        );
        return $this->success($data);
    }




}
